import logger from "../utils/logger";
import { dbName, dbPath } from "./constant";

// 打开或创建数据库
function openDB() {
	return new Promise<boolean>((resolve) => {
		plus.sqlite.openDatabase({
			name: dbName, // 数据库名称
			path: dbPath, // 数据库文件路径
			success: () => {
				logger('数据库打开/创建成功');
				resolve(true);
			},
			fail: (e) => {
				if(e.code === -1402){
					logger('数据库已打开');
					return resolve(true);
				}
				logger('数据库打开失败:', e);
				resolve(false);
			}
		});
	});
}

export default openDB;